<?php

namespace app\controller;


use app\model\SchoolRoleModel;
use think\Request;
use app\middleware\Check;
use think\facade\Validate;
use think\response\Json;
use think\validate\ValidateRule;

class SchoolRole
{
    protected $middleware = [Check::class];

    /**列表
     * @param Request $request
     * @return Json
     */
    public function list(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');//每页显示数量
        $input['page'] = $request->post('page');//当前页
        $input['name'] = $request->post('name');//
        $input['school_id'] = $request->post('school_id');//学校id
        $input['type'] = $request->post('type');//类型
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'type' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } else if (in_array($value, ['', '1', '2', '3', '4'], true)) {
                    return true;
                } else {
                    return '参数错误';
                }
            },
            'name' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } else {
                    return true;
                }
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return SchoolRoleModel::list($input, $request->post('token'));
    }

    /**添加
     * @param Request $request
     * @return Json
     */
    public function add(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['school_id'] = $request->post('school_id');//学校id
        $input['type'] = $request->post('type');//类型
        $input['name'] = $request->post('name');//
        $input['phone'] = $request->post('phone');//类型
        // 参数校验
        $validate = Validate::rule([
            'phone' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'type' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } else if (in_array($value, ['1', '2', '3', '4'], true)) {
                    return true;
                } else {
                    return '参数错误';
                }
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return SchoolRoleModel::add($input, $request->post('token'));
    }

    /**修改管理员状态
     * @param Request $request
     * @return Json
     */
    public function changeStatus(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['school_id'] = $request->post('school_id');//学校id
        $input['school_role_id'] = $request->post('school_role_id');
        $input['status'] = $request->post('status');
        // 参数校验
        $validate = Validate::rule([
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_role_id' => ValidateRule::isRequire(null, '缺少参数'),
            'status' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } else if (in_array($value, ['0', '1'], true)) {
                    return true;
                } else {
                    return '参数错误';
                }
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return SchoolRoleModel::changeStatus($input, $request->post('token'));
    }

    /**获取单个管理员
     * @param Request $request
     * @return Json
     */
    public function getOnly(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['school_id'] = $request->post('school_id');//学校id
        $input['id'] = $request->post('id');
        // 参数校验
        $validate = Validate::rule([
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'id' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return SchoolRoleModel::getOnly($input, $request->post('token'));
    }

    /**编辑管理员
     * @param Request $request
     * @return Json
     */
    public function edit(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['name'] = $request->post('name');
        $input['phone'] = $request->post('phone');
        $input['school_id'] = $request->post('school_id');//学校id
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'phone' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return SchoolRoleModel::edit($input, $request->post('token'));
    }

    /**删除管理员
     * @param Request $request
     * @return Json
     */
    public function delete(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['school_id'] = $request->post('school_id');//学校id
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数')
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return SchoolRoleModel::delete($input, $request->post('token'));
    }

    /**获取对应类型管理员
     * @param Request $request
     * @return Json
     */
    public function getRoleTypeList(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['school_id'] = $request->post('school_id');//学校id
        $input['type'] = $request->post('type');
        // 参数校验
        $validate = Validate::rule([
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
            'type' => function($v) {
                return in_array($v, ['1','2','3','4'], true) ? true: '不合法参数';
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return SchoolRoleModel::getRoleTypeList($input, $request->post('token'));
    }

    /**商家列表
     * @param Request $request
     * @return Json
     */
    public function store_list(Request $request)
    {
        $input['school_id'] = $request->post('school_id');//每页显示数量
        // 参数校验
        $validate = Validate::rule([
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return SchoolRoleModel::Store_list($input);

    }

    /**订单列表
     * @param Request $request
     * @return Json
     */
    public function order_list(Request $request)
    {
        $input['store_id'] = $request->post('store_id');//每页显示数量
        $input['status'] = $request->post('status');//0:未结算 1：已结算 2：全部订单
        $input['start_time'] = $request->param('start_time');//开始时间
        $input['end_time'] = $request->param('end_time');//结束时间
        // 参数校验
        $validate = Validate::rule([
            'store_id' => ValidateRule::isRequire(null, '缺少参数'),
            'start_time' => ValidateRule::isRequire(null, '缺少参数'),
            'end_time' => ValidateRule::isRequire(null, '缺少参数'),
            'status' => function ($value) {
                if (in_array($value,[0,1,2])) {
                    return true;
                } else {
                    return '参数类型错误';
                }
            },
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return SchoolRoleModel::Order_list($input);
    }

    /**订单列表
     * @param Request $request
     * @return Json
     */
    public function order_modify(Request $request)
    {
        $input['ids'] = $request->post('ids');//订单ID组合 1,2
        // 参数校验
        $validate = Validate::rule([
            'ids' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return SchoolRoleModel::order_modify($input);
    }
}